﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conference.Logic;
using System.Globalization;
using Conference.Logic.Base;
using Conference.Logic.Proposal;
using Conference.Logic.Schedule;

namespace Conference.Console
{
    class Program
    {
        /// <summary>
        /// Main program
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                // plan conference
                Proposals proposals = new Proposals(true);
                Event conference = new Logic.Schedule.Event("Sample Conference");

                // schedule all proposals according with available time
                conference.Arrange(proposals);

                // show in the screen
                ShowSchedule(conference);
                System.Console.ReadKey();
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        }
        
        /// <summary>
        /// Display all tracks ans sessions on screen for the conference
        /// </summary>
        /// <param name="conference"></param>
        static void ShowSchedule(Conference.Logic.Schedule.Event conference)
        {
            try
            {
                System.Console.Clear();
                System.Console.WriteLine(Environment.NewLine);
                System.Console.WriteLine("CONFERENCE: {0} {1}", conference.Name, Environment.NewLine);

                foreach (Track track in conference.Tracks)
                {
                    System.Console.WriteLine("{1}Track {0}:{1}", track.Day.ToString(), Environment.NewLine);

                    ShowSessions(track.Moorning.Sessions);
                    ShowSessions(track.Afternoon.Sessions);
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        }

        /// <summary>
        /// Display all session itens inside the list
        /// </summary>
        /// <param name="list">List of item session</param>
        static void ShowSessions(List<ItemSession> list)
        {
            try
            {
                foreach (var item in list)
                {
                    if (item.Type == ItemType.Default)
                    {
                        System.Console.Write(string.Format(" [{0}] {1} - {2}min {3}",
                            DateTime.Today.Add(item.StartTime).ToString("hh:mm tt"),
                            item.Description,
                            item.Duration.TotalMinutes,
                            Environment.NewLine));
                    }
                    else if (item.Type == ItemType.Lightning)
                    {
                        System.Console.Write(string.Format(" [{0}] {1} - {2} {3}",
                            DateTime.Today.Add(item.StartTime).ToString("hh:mm tt"),
                            item.Description,
                            "Lightning",
                            Environment.NewLine));                    
                    }
                    else
                    {
                        System.Console.Write(string.Format(" [{0}] {1} {2}",
                            DateTime.Today.Add(item.StartTime).ToString("hh:mm tt"),
                            item.Description,
                            Environment.NewLine));
                    }
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        }

        /// <summary>
        /// Manage errors and show a message to user
        /// </summary>
        /// <param name="ex">Exception</param>
        static void ShowError(Exception ex)
        {
            System.Console.Write(Environment.NewLine);
            System.Console.ForegroundColor = ConsoleColor.Red;
            System.Console.Write("An error has happened: {0}.{1}Try again.", ex.Message, Environment.NewLine);
            System.Console.ReadKey();
        }
    }
}
